package org.fh.controller.fhoa;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.fh.controller.base.BaseController;
import org.fh.entity.Page;
import org.fh.entity.PageData;
import org.fh.service.fhoa.IndustryService;
import org.fh.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@RequestMapping("/industry")
@RestController
public class IndustryController extends BaseController {

    @Autowired
    private IndustryService industryService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 保存
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/add")
    @RequiresPermissions("industry:add")
    @ResponseBody
    public Object add(
            @RequestParam(value = "INDUSTRYID", required = false) String INDUSTRYID,
            @RequestParam(value = "PARENT_ID", required = false) String PARENT_ID,
            @RequestParam(value = "INDUSTRY_NAME", required = false) String INDUSTRY_NAME,
            @RequestParam(value = "INDUSTRY_FULL_NAME", required = false) String INDUSTRY_FULL_NAME,
            @RequestParam(value = "INDUSTRYTYPE", required = false) String INDUSTRYTYPE,
            @RequestParam(value = "SORTNUMBER", required = false) String SORTNUMBER,
            @RequestParam(value = "COMMENTS", required = false) String COMMENTS,
            @RequestParam(value = "CREATETIME", required = false) String CREATETIME,
            @RequestParam(value = "UPDATETIME", required = false) String UPDATETIME,
            @RequestParam(value = "STATE", required = false) String STATE


    ) throws Exception {

        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        String ffile = DateUtil.getDays(), fileNamea = "";
        String filePath = PathUtil.getHttpUrlpathFile() + Const.FILEPATHIMG + ffile;     //文件上传路径
        String readFilePath = PathUtil.getHttpUrlpath(request) + Const.FILEPATHIMG + ffile;


        //写入表
        pd.put("INDUSTRYID", INDUSTRYID);
        pd.put("PARENT_ID", PARENT_ID);
        pd.put("INDUSTRY_NAME", INDUSTRY_NAME);
        pd.put("INDUSTRY_FULL_NAME", INDUSTRY_FULL_NAME);
        pd.put("INDUSTRYTYPE", INDUSTRYTYPE);
        pd.put("SORTNUMBER", SORTNUMBER);
        pd.put("COMMENTS", COMMENTS);
        pd.put("CREATETIME", CREATETIME);
        pd.put("CREATETIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("UPDATETIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("STATE", STATE);

        pd.put("USERNAME", Jurisdiction.getUsername());    //
        pd.put("INDUSTRYID", this.get32UUID());                    //主键
        pd.put("UNAME", Jurisdiction.getName());
        pd.put("USERNAME", Jurisdiction.getUsername());

        pd.put("INDUSTRYID", "");
//			pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());


        industryService.save(pd);
//
//        try {
//            /** 工作流的操作 **/
//            Map<String,Object> mapa = new LinkedHashMap<String, Object>();
//            mapa.put("申请人员", Jurisdiction.getName());			//当前用户的姓名
//            mapa.put("开始时间", BEIGINTIME);
//            mapa.put("结束时间", ENDTIME);
//            mapa.put("申请标题", LOANTITLE);
//            mapa.put("申请内容", LOANDESC);
//            mapa.put("申请金额", AMOUNT);
//            String USERNAME = Jurisdiction.getUsername();
//            mapa.put("USERNAME", USERNAME);						//指派代理人为当前用户
////            startProcessInstanceByKeyHasVariables("KEY_leave",mapa,USERNAME);//启动流程实例(请假单流程)通过KEY
//            //记录存入数据库
//            map.put("ASSIGNEE_",Jurisdiction.getUsername());				//用于给待办人发送新任务消息
//        } catch (Exception e) {
//            errInfo = "errer";
//        }
        errInfo = "errer";
        map.put("result", errInfo);                //返回结果
        return map;
    }


    /**
     * 删除
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @RequiresPermissions("industry:del")
    @ResponseBody
    public Object delete() throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        industryService.delete(pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }


    /**
     * 修改
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @RequiresPermissions("industry:edit")
    @ResponseBody
    public Object edit(
            @RequestParam(value = "INDUSTRYID", required = false) String INDUSTRYID,
            @RequestParam(value = "PARENT_ID", required = false) String PARENT_ID,
            @RequestParam(value = "INDUSTRY_NAME", required = false) String INDUSTRY_NAME,
            @RequestParam(value = "INDUSTRY_FULL_NAME", required = false) String INDUSTRY_FULL_NAME,
            @RequestParam(value = "INDUSTRYTYPE", required = false) String INDUSTRYTYPE,
            @RequestParam(value = "SORTNUMBER", required = false) String SORTNUMBER,
            @RequestParam(value = "COMMENTS", required = false) String COMMENTS,
            @RequestParam(value = "CREATETIME", required = false) String CREATETIME,
            @RequestParam(value = "USERNAME", required = false) String USERNAME,
            @RequestParam(value = "UPDATETIME", required = false) String UPDATETIME,
            @RequestParam(value = "STATE", required = false) String STATE

    ) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        String ffile = DateUtil.getDays(), fileNamea = "";
        String filePath = PathUtil.getHttpUrlpathFile() + Const.FILEPATHIMG + ffile;     //文件上传路径
        String readFilePath = PathUtil.getHttpUrlpath(request) + Const.FILEPATHIMG + ffile;

//
//        if (null != filea && !filea.isEmpty()) {
//            fileNamea = FileUpload.fileUp(filea, filePath, this.get32UUID());                //执行上传
//            pd.put("PHOTOADDA", readFilePath + "/" + fileNamea);
//        }


        pd.put("PARENT_ID", PARENT_ID);
        pd.put("INDUSTRY_NAME", INDUSTRY_NAME);
        pd.put("INDUSTRY_FULL_NAME", INDUSTRY_FULL_NAME);
        pd.put("INDUSTRYTYPE", INDUSTRYTYPE);
        pd.put("SORTNUMBER", SORTNUMBER);
        pd.put("COMMENTS", COMMENTS);
        pd.put("CREATETIME", DateUtil.date2Str(new Date()));    //创建时间
        pd.put("UPDATETIME", UPDATETIME);
        pd.put("STATE", STATE);
        pd.put("USERNAME", Jurisdiction.getUsername());    //
        pd.put("INDUSTRYID", INDUSTRYID);                    //主键
//		pd.put("UNAME", Jurisdiction.getName());
//		pd.put("USERNAME", Jurisdiction.getUsername());
//		pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
        industryService.edit(pd);

        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 去修改页面
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goEdit")
    @ResponseBody
    public Object goEdit() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd = industryService.findById(pd);    //根据ID读取
        map.put("pd", pd);
        map.put("result", errInfo);                //返回结果
        return map;
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    @RequiresPermissions("industry:list")
    @ResponseBody
    public Object list(Page page) throws Exception {
//		Map<String,Object> map = new HashMap<String,Object>();
//		String errInfo = "success";
//		PageData pd = new PageData();
//		pd = this.getPageData();
//
//		String USERNAME = Jurisdiction.getUsername();					//用户名
//		page.setPd(pd);
//		List<PageData>	varList = industryService.list(page);
//		map.put("varList", varList);
//		map.put("page", page);
//		map.put("USERNAME", USERNAME);
//		map.put("result", errInfo);
//		return map;
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String isMy = pd.getString("isMy");
        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        String lastStart = pd.getString("lastStart");                    //开始时间
        String lastEnd = pd.getString("lastEnd");                        //结束时间
        if (Tools.notEmpty(lastStart)) pd.put("lastStart", lastStart + " 00:00:00");
        if (Tools.notEmpty(lastEnd)) pd.put("lastEnd", lastEnd + " 00:00:00");
        String USERNAME = Jurisdiction.getUsername();                    //用户名
        String item = Jurisdiction.getDEPARTMENT_IDS();

        boolean isAdminLike = "韩彦聪".equals(USERNAME) || "admin".equals(USERNAME);
        if ("false".equals(isMy)) {
            if (isAdminLike) {
                pd.put("item", "");
                pd.remove("USERNAME");
            } else {
                if ("".equals(item) || "无权".equals(item)) {
                    pd.put("item", "");                                            //根据部门ID过滤
                    if (!"admin".equals(USERNAME)) pd.put("USERNAME", USERNAME);    //非admin用户时
                } else {
                    pd.put("ORUSERNAME", USERNAME);
                    pd.put("item", item.replaceFirst("\\(", "\\('" + Jurisdiction.getDEPARTMENT_ID() + "',"));
                }
            }
        } else {
            if (!isAdminLike) {
                pd.put("USERNAME", USERNAME);
            } else {
                pd.put("USERNAME", USERNAME);
            }
        }


        page.setPd(pd);
        List<PageData> varList = industryService.list(page);            //列出Worklog列表
        map.put("varList", varList);
        map.put("page", page);
        map.put("USERNAME", USERNAME);
        map.put("result", errInfo);

        return map;
    }

    /**
     * 导出到excel
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/excel")
    @RequiresPermissions("toExcel")
    public ModelAndView exportExcel() throws Exception {
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        pd = this.getPageData();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<String> titles = new ArrayList<String>();
        titles.add("行业名称");    //1
        titles.add("行业全称");    //2
        titles.add("行业类型");    //3
        titles.add("备注");    //5
        titles.add("创建时间");    //6
        titles.add("修改时间");    //7
        titles.add("排序号");    //12
        dataMap.put("titles", titles);
        List<PageData> varOList = industryService.listAll(pd);
        List<PageData> varList = new ArrayList<PageData>();
        for (int i = 0; i < varOList.size(); i++) {
            PageData vpd = new PageData();
            vpd.put("var1", varOList.get(i).getString("PARENT_ID"));        //1
            vpd.put("var2", varOList.get(i).getString("INDUSTRY_NAME"));        //2
            vpd.put("var3", varOList.get(i).getString("INDUSTRY_FULL_NAME"));        //2
            vpd.put("var4", varOList.get(i).getString("INDUSTRYTYPE"));        //2
            vpd.put("var5", varOList.get(i).getString("SORTNUMBER"));        //2
            vpd.put("var6", varOList.get(i).getString("COMMENTS"));        //2
            vpd.put("var7", varOList.get(i).getString("CREATETIME"));        //3
            vpd.put("var8", varOList.get(i).getString("UPDATETIME"));        //4
            vpd.put("var8", varOList.get(i).getString("STATE"));        //4

            varList.add(vpd);
        }
        dataMap.put("varList", varList);
        ObjectExcelView erv = new ObjectExcelView();
        mv = new ModelAndView(erv, dataMap);
        return mv;
    }
}
